﻿Public Class ucManageStaff
	Inherits System.Web.UI.UserControl

	Event Alert(ByVal msg As String)

	Enum ModeType
		Add
		Edit
		View
	End Enum

	Public Property Mode As ModeType
		Get
			Return Me.ViewState("mode")
		End Get
		Set(ByVal value As ModeType)
			Me.ViewState("mode") = value
		End Set
	End Property

	Private Sub InitJS()
        Dim sbJS As New Text.StringBuilder
        sbJS.AppendLine("var brithdayID = '" & Me.OFFICER_BIRTHDATE.ClientID & "';")
        sbJS.AppendLine("var carddayID = '" & Me.OFFICER_CARD_EXPIREDATE.ClientID & "';")
        sbJS.AppendLine("var appID = '" & Me.OFFICER_APP_ID.ClientID & "';")
        sbJS.AppendLine("var cardID = '" & Me.OFFICER_CARD_NO.ClientID & "';")
        sbJS.AppendLine("var cardTypeDesc = '" & Me.OFFICER_CARD_TYPE_DESC.ClientID & "';")
        Me.ltrJS.Text = "<script language=""JavaScript"">" & sbJS.ToString & "</script>"
	End Sub

	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		If Not Page.IsPostBack Then
			LoadProvince1()
			LoadDistinct1()
			LoadSubDistinct1()
			LoadPostCode1()

			If Mode = ModeType.Add Then
				Me.ltrHeader.Text = "เพิ่มข้อมูลพนักงาน"
				Me.trPWD1.Visible = False
                Me.trPWD2.Visible = False
                Me.btnAuthorizeSetting.Visible = False
			ElseIf Mode = ModeType.View Then
				Me.ltrHeader.Text = "แสดงข้อมูลพนักงาน"
				Me.trPWD1.Visible = False
				Me.trPWD2.Visible = False
                Me.btnSave.Visible = False
                Me.btnAuthorizeSetting.Visible = False
				If GlobalApp.Authen IsNot Nothing Then
					LoadData(BL.Officer.LoadProfile(GlobalApp.Authen.offcier_id))
				Else
					Throw New Exception("ไม่สามารถดึงข้อมูล Staff จากฐานข้อมูลได้ เนื่องจากไม่พบ Officer ID ที่ได้จากการ Login")
				End If
				EnableControl(False)
			Else
				Me.ltrHeader.Text = "เปลี่ยนแปลงแก้ไขข้อมูลพนักงาน"
				Me.trPWD1.Visible = True
				Me.trPWD2.Visible = True
                Me.OFFICER_APP_ID.Enabled = False
                Me.btnAuthorizeSetting.Visible = True
				If GlobalApp.DataSourceEditOfficer IsNot Nothing Then
					LoadData(GlobalApp.DataSourceEditOfficer)
				End If
			End If
		End If

		InitJS()
	End Sub

	Private Sub EnableControl(ByVal value As Boolean)
		OFFICER_APP_ID.Enabled = value
		OFFICER_FIRSTNAME_THA.Enabled = value
		OFFICER_LASTNAME_THA.Enabled = value
		OFFICER_FIRSTNAME_ENG.Enabled = value
		OFFICER_LASTNAME_ENG.Enabled = value
        OFFICER_SEX.Enabled = value
        ddlCardType.Enabled = value
        OFFICER_CARD_TYPE_DESC.Enabled = value
		OFFICER_CARD_NO.Enabled = value
        OFFICER_BIRTHDATE.Enabled = value
        OFFICER_CARD_EXPIREDATE.Enabled = value
		OFFICER_CARD_EXPIREDATE_TYPE.Enabled = value
		OFFICER_MARRIED_STATUS.Enabled = value
		OFFICER_CARD_ADDRESS_NO.Enabled = value
		OFFICER_CARD_ADDRESS_MOO.Enabled = value
		OFFICER_CARD_ADDRESS_BUILDING.Enabled = value
		OFFICER_CARD_ADDRESS_SOI.Enabled = value
		OFFICER_CARD_ADDRESS_STREET.Enabled = value
		OFFICER_CARD_ADDRESS_PROVINCE.Enabled = value
		OFFICER_CARD_ADDRESS_DISTRICT.Enabled = value
		officer_card_address_subICT.Enabled = value
		OFFICER_CARD_ADDRESS_POSTCODE.Enabled = value
		OFFICER_MAIL_PHONE.Enabled = value
		OFFICER_MAIL_EMAIL.Enabled = value
		ddlStatus.Enabled = value
		OFFICER_REMARK.Enabled = value
	End Sub

	Private Sub LoadProvince1()
		OFFICER_CARD_ADDRESS_PROVINCE.DataTextField = "PROVINCE_NAME"
		OFFICER_CARD_ADDRESS_PROVINCE.DataValueField = "PROVINCE_ID"
		OFFICER_CARD_ADDRESS_PROVINCE.DataSource = BL.Province.GetAll
		OFFICER_CARD_ADDRESS_PROVINCE.DataBind()
		OFFICER_CARD_ADDRESS_PROVINCE.Items.Insert(0, New ListItem("--กรุณาระบุจังหวัด--", "0"))
	End Sub

	Private Sub LoadDistinct1()
		If Not Me.OFFICER_CARD_ADDRESS_PROVINCE.SelectedValue.Equals("0") Then
			OFFICER_CARD_ADDRESS_DISTRICT.DataTextField = "DISTRICT_NAME"
			OFFICER_CARD_ADDRESS_DISTRICT.DataValueField = "DISTRICT_ID"
			OFFICER_CARD_ADDRESS_DISTRICT.DataSource = BL.District.GetAll(Me.OFFICER_CARD_ADDRESS_PROVINCE.SelectedValue)
			OFFICER_CARD_ADDRESS_DISTRICT.DataBind()
		Else
			OFFICER_CARD_ADDRESS_DISTRICT.Items.Clear()
		End If
		OFFICER_CARD_ADDRESS_DISTRICT.Items.Insert(0, New ListItem("--กรุณาระบุเขต/อำเภอ--", "0"))
	End Sub

	Private Sub LoadSubDistinct1()
		If Not Me.OFFICER_CARD_ADDRESS_DISTRICT.SelectedValue.Equals("0") Then
			officer_card_address_subICT.DataTextField = "SUB_DISTRICT_NAME"
			officer_card_address_subICT.DataValueField = "SUB_DISTRICT_ID"
			officer_card_address_subICT.DataSource = BL.SubDistrict.AllSubDistrict(Me.OFFICER_CARD_ADDRESS_PROVINCE.SelectedValue, Me.OFFICER_CARD_ADDRESS_DISTRICT.SelectedValue)
			officer_card_address_subICT.DataBind()
		Else
			officer_card_address_subICT.Items.Clear()
		End If
		officer_card_address_subICT.Items.Insert(0, New ListItem("--กรุณาระบุแขวง/ตำบล--", "0"))
	End Sub

	Private Sub LoadPostCode1()
		If Not Me.officer_card_address_subICT.SelectedValue.Equals("0") Then
			OFFICER_CARD_ADDRESS_POSTCODE.DataTextField = "POSTCODE"
			OFFICER_CARD_ADDRESS_POSTCODE.DataValueField = "POSTCODE"
			OFFICER_CARD_ADDRESS_POSTCODE.DataSource = BL.PostCode.AllPostCode(Me.OFFICER_CARD_ADDRESS_PROVINCE.SelectedValue, Me.OFFICER_CARD_ADDRESS_DISTRICT.SelectedValue)
			OFFICER_CARD_ADDRESS_POSTCODE.DataBind()
		Else
			OFFICER_CARD_ADDRESS_POSTCODE.Items.Clear()
		End If
		OFFICER_CARD_ADDRESS_POSTCODE.Items.Insert(0, New ListItem("--รหัสไปรษณีย์--", "0"))
	End Sub

	Protected Sub OFFICER_CARD_ADDRESS_PROVINCE_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles OFFICER_CARD_ADDRESS_PROVINCE.SelectedIndexChanged
		Me.LoadDistinct1()
		Me.LoadSubDistinct1()
		Me.LoadPostCode1()
	End Sub

	Protected Sub OFFICER_CARD_ADDRESS_DISTRICT_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles OFFICER_CARD_ADDRESS_DISTRICT.SelectedIndexChanged
		Me.LoadSubDistinct1()
		Me.LoadPostCode1()
	End Sub

	Protected Sub officer_card_address_subICT_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles officer_card_address_subICT.SelectedIndexChanged
		Me.LoadPostCode1()
	End Sub

	Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
        Dim registerInfo As Entity.OfficerInfoItem
		If GlobalApp.DataSourceEditOfficer IsNot Nothing Then
			registerInfo = GlobalApp.DataSourceEditOfficer
		Else
			registerInfo = New Entity.OfficerInfoItem
		End If

		With registerInfo
			.officer_app_id = Me.OFFICER_APP_ID.Text
			.officer_firstname_tha = Me.OFFICER_FIRSTNAME_THA.Text
			.officer_lastname_tha = Me.OFFICER_LASTNAME_THA.Text
			.officer_firstname_eng = Me.OFFICER_FIRSTNAME_ENG.Text
			.officer_lastname_eng = Me.OFFICER_LASTNAME_ENG.Text
            .officer_sex = Me.OFFICER_SEX.SelectedValue
            .officer_card_type = Me.ddlCardType.SelectedValue
            If Me.ddlCardType.SelectedValue <> "OTH" Then OFFICER_CARD_TYPE_DESC.Text = ""
            .officer_card_type_desc = Me.OFFICER_CARD_TYPE_DESC.Text.Trim
            .officer_card_no = Me.OFFICER_CARD_NO.Text
            '.officer_birthdate = New Date(Me.OFFICCER_BIRTHDATE.Year, Me.OFFICCER_BIRTHDATE.Month, Me.OFFICCER_BIRTHDATE.Day)
            .officer_birthdate = Common.UtilityDateTime.ConvertDate(Me.OFFICER_BIRTHDATE.Text).Value.AddYears(-543)

            Try
                If Not String.IsNullOrEmpty(Me.OFFICER_CARD_EXPIREDATE.Text) Then
                    If Common.UtilityDateTime.ConvertDate(Me.OFFICER_CARD_EXPIREDATE.Text).HasValue Then
                        '.officer_card_expiredate = New Date(Me.OFFICCER_CARD_EXPIREDATE.Year, Me.OFFICCER_CARD_EXPIREDATE.Month, Me.OFFICCER_CARD_EXPIREDATE.Day)
                        .officer_card_expiredate = Common.UtilityDateTime.ConvertDate(Me.OFFICER_CARD_EXPIREDATE.Text).Value.AddYears(-543)
                    Else
                        RaiseEvent Alert("กรุณาระบุวันบัตรหมดอายุให้ถูกรูปแบบ (วว/ดด/ปปปป)")
                        Exit Sub
                    End If
                End If
            Catch
            End Try
            .officer_card_expiredate_type = Me.OFFICER_CARD_EXPIREDATE_TYPE.SelectedValue
            .officer_married_status = Me.OFFICER_MARRIED_STATUS.SelectedValue
            .officer_card_address_no = Me.OFFICER_CARD_ADDRESS_NO.Text
            .officer_card_address_moo = Me.OFFICER_CARD_ADDRESS_MOO.Text
            .officer_card_address_building = Me.OFFICER_CARD_ADDRESS_BUILDING.Text
            .officer_card_address_soi = Me.OFFICER_CARD_ADDRESS_SOI.Text
            .officer_card_address_street = Me.OFFICER_CARD_ADDRESS_STREET.Text
            .officer_card_address_province = Me.OFFICER_CARD_ADDRESS_PROVINCE.SelectedItem.Text
            .officer_card_address_district = Me.OFFICER_CARD_ADDRESS_DISTRICT.SelectedItem.Text
            .officer_card_address_sub = Me.officer_card_address_subICT.SelectedItem.Text
            .officer_card_address_postcode = Me.OFFICER_CARD_ADDRESS_POSTCODE.SelectedItem.Text
            .officer_mail_phone = Me.OFFICER_MAIL_PHONE.Text
            .officer_mail_email = Me.OFFICER_MAIL_EMAIL.Text
            .officer_status = Me.ddlStatus.SelectedValue
            .officer_remark = Me.OFFICER_REMARK.Text
            If GlobalApp.Authen IsNot Nothing Then
                .create_user_id = GlobalApp.Authen.offcier_id
                .upd_user_id = GlobalApp.Authen.offcier_id
            End If
        End With

		Try

			If Mode = ModeType.Add Then
				BL.Officer.AddStaff(registerInfo)
				GlobalApp.DepositMessage = "เพิ่มข้อมูลพนักงานเรียบร้อยแล้ว"
			Else
				Dim useridInfo As Entity.UserInfoItem = Nothing
				If Not String.IsNullOrEmpty(Me.txtPassword.Text) Then
					useridInfo = New Entity.UserInfoItem
					useridInfo.user_password = Me.txtPassword.Text
				End If
				BL.Officer.UpdateStaff(registerInfo, useridInfo)
				GlobalApp.DepositMessage = "แก้ไขข้อมูลพนักงานเรียบร้อยแล้ว"
			End If
			Response.Redirect("~/OfficerManagement.aspx", False)
		Catch ex As Exception
			RaiseEvent Alert(ex.Message)
		End Try
	End Sub

	Private Sub LoadData(ByVal datasource As Entity.OfficerInfoItem)

		If datasource Is Nothing Then
			Throw New Exception("ไม่พบข้อมูล Staff ที่นำมาแสดงผล")
		End If

		Dim registerInfo As Entity.OfficerInfoItem
		registerInfo = datasource
		With registerInfo
			Me.OFFICER_APP_ID.Text = .officer_app_id
			Me.OFFICER_FIRSTNAME_THA.Text = .officer_firstname_tha
			Me.OFFICER_LASTNAME_THA.Text = .officer_lastname_tha
			Me.OFFICER_FIRSTNAME_ENG.Text = .officer_firstname_eng
			Me.OFFICER_LASTNAME_ENG.Text = .officer_lastname_eng
            Common.Utility.SelectChoice(Me.OFFICER_SEX, .officer_sex)
            Me.ddlCardType.SelectedValue = .officer_card_type
            If Me.ddlCardType.SelectedValue = "OTH" Then OFFICER_CARD_TYPE_DESC.Enabled = True
            Me.OFFICER_CARD_TYPE_DESC.Text = .officer_card_type_desc
            Me.OFFICER_CARD_NO.Text = .officer_card_no
            Try
                If Not IsNothing(.officer_birthdate) Then Me.OFFICER_BIRTHDATE.Text = .officer_birthdate.Value.AddYears(543).ToString("dd/MM/yyyy", Common.Utility.CulInfo)
            Catch
            End Try
            Try
                If Not IsNothing(.officer_card_expiredate) Then Me.OFFICER_CARD_EXPIREDATE.Text = .officer_card_expiredate.Value.AddYears(543).ToString("dd/MM/yyyy", Common.Utility.CulInfo)
            Catch
            End Try
            Common.Utility.SelectChoice(Me.OFFICER_CARD_EXPIREDATE_TYPE, .officer_card_expiredate_type)
            Common.Utility.SelectChoice(Me.OFFICER_MARRIED_STATUS, .officer_married_status)
            Common.Utility.SelectChoice(Me.ddlStatus, .officer_status)

            Me.OFFICER_CARD_ADDRESS_NO.Text = .officer_card_address_no
            Me.OFFICER_CARD_ADDRESS_MOO.Text = .officer_card_address_moo
            Me.OFFICER_CARD_ADDRESS_BUILDING.Text = .officer_card_address_building
            Me.OFFICER_CARD_ADDRESS_SOI.Text = .officer_card_address_soi
            Me.OFFICER_CARD_ADDRESS_STREET.Text = .officer_card_address_street
            Common.Utility.SelectChoiceByText(OFFICER_CARD_ADDRESS_PROVINCE, .officer_card_address_province)
            Me.LoadDistinct1()
            Common.Utility.SelectChoiceByText(OFFICER_CARD_ADDRESS_DISTRICT, .officer_card_address_district)
            Me.LoadSubDistinct1()
            Common.Utility.SelectChoiceByText(officer_card_address_subICT, .officer_card_address_sub)
            Me.LoadPostCode1()
            Common.Utility.SelectChoiceByText(OFFICER_CARD_ADDRESS_POSTCODE, .officer_card_address_postcode)
            Me.OFFICER_MAIL_PHONE.Text = .officer_mail_phone
            Me.OFFICER_MAIL_EMAIL.Text = .officer_mail_email
            Common.Utility.SelectChoiceByText(Me.ddlStatus, .officer_status)
            Me.OFFICER_REMARK.Text = .officer_remark
        End With
	End Sub

	Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCancel.Click
		If Mode = ModeType.View Then
			Response.Redirect("~/MainPage.aspx", False)
		Else
			Response.Redirect("~/OfficerManagement.aspx", False)
		End If

    End Sub

    Protected Sub ddlCardType_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlCardType.SelectedIndexChanged
        Try
            If ddlCardType.SelectedValue = "OTH" Then
                lblCardType.Text = "เลขที่บัตรอื่นๆ *:"
                OFFICER_CARD_TYPE_DESC.Enabled = True
            Else
                OFFICER_CARD_TYPE_DESC.Text = ""
                If ddlCardType.SelectedValue = "PID" Then lblCardType.Text = "เลขที่บัตรประชาชน *:"
                If ddlCardType.SelectedValue = "CID" Then lblCardType.Text = "เลขที่บัตรนิติบุคคล *:"
                If ddlCardType.SelectedValue = "FID" Then lblCardType.Text = "เลขที่บัตรคนต่างชาติ *:"
                OFFICER_CARD_TYPE_DESC.Text = ""
                OFFICER_CARD_TYPE_DESC.Enabled = False
            End If
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub btnAuthorizeSetting_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAuthorizeSetting.Click
        Response.Redirect("~/EditStaffAuthorize.aspx?officerappid=" & OFFICER_APP_ID.Text.Trim, False)
    End Sub

End Class